<?php
namespace dg\Controller;
use Think\Controller;

class UserController extends ApibaseController {
	/*
	 * 登陆模块
	 */
	public function login(){
		if(is_login()){
            $role_id =session('role_id');
            if($role_id == 3){
                //套餐管理账号 12-20新增
                redirect('/project/index?res=0-1');
            }
            redirect('/consume/index?res=0-1');
        }else{
    		$this->display('user/login');
        }
    }

    /*
     * 系统登陆
     */
    public function checklogin(){
    	$userMsg = M('ResEntity')->where(array('username'=>I('post.username'),'is_cash'=>0))->find();
        if(!$userMsg){
            jsonRespons('0','您的账号不存在！');
        }
        if(!$userMsg['status']){
            jsonRespons('0','您的账号被禁用，请联系客服！');
        }
    	if($userMsg['password'] == md5(I('post.passwd').$userMsg['salt'])){
    		$pushSession = array('name'=>I('post.username'));
    		if(I('post.signlogin') == 1){
    			$setSecret = md5(I('post.username').'ajwUid'.time());
    			M('ResEntity')->where(array('username'=>I('post.username')))->setField(
    				array('remember_token' => $setSecret)
    			);
    			cookie('_ajwunion',$setSecret,'expire=604800');
    			$pushSession['expire'] = 604800;
    		}
            $role_id = M('AuthGroupAccess')->where(array('uid'=>$userMsg['rid']))->field('role_id')->find();
    		session($pushSession);
            session('role_id',$role_id['role_id']);
    		session('loginUid',$userMsg['rid']);
    		session('loginName',I('post.username'));
    		cookie('ajwUid',base64_encode(I('post.username')).randstr(),'expire=604800');
    		session(I('post.username'),true);
    		jsonRespons('1','登陆成功！');
    	}else{
    		jsonRespons('0','帐号或密码有误！');
    	}
    }

	
    /*
     * 系统注消操作
     */
    public function loginout(){
    	$userMsg = getLoginMsg();
    	session(cookie('PHPSESSID'),null);
        session('role_id',null);
    	session(getLoginMsg()->username,null);
        session('loginUid',null);
        session('loginName',null);
    	cookie('ajwUid',null);
    	cookie('_ajwunion',null);
    	cookie('PHPSESSID',null);

    	redirect('/user/login');
    }
    
    /*
     * 个人中心简介
     */
    public function usercenter(){
    	//个人页面渲染
    	$this->display('user/usercenter');
    }
    
    /*
     * 个人中心资料编辑
     */
    public function userUpdate(){
    	if(I('post.update')){
    		$data = array('email'=>I('post.email'));
    		if(I('post.password')){
    			$salt = randstr(5);
    			$data['password'] = md5(I('post.password').$salt);
    			$data['salt'] = $salt;
    		}
    		$data['email'] = I('post.email');
    		session('kfLine'.I('post.rid'),I('post.lineStatus') == '1' ? '1' : null);
    		$keArr = session('kfLineArr');
    		if(I('post.lineStatus') == '1'){
    			if(isset($keArr[I('post.rid')]) && $keArr[I('post.rid')] = I('post.rid')){
    				// Nothing to do
    			}else{
    				$keArr[I('post.rid')] = I('post.rid');
    			}
    			session('kfLineArr',$keArr);
    		}else{
    			if(isset($keArr[I('post.rid')])){
    				unset($keArr[I('post.rid')]);
    				session('kfLineArr',$keArr);
    			}
    		}
    		M('ResEntity')->where(array('rid'=>session('loginUid')))->setField($data);
    		$this->showtips('信息编辑成功');
    	}
    }



    /*
     * 后台管理员管理
     * @date: 2016-8-2 上午10:14:13
     * @editor: liuqi
     */
    public function admin(){
        $page = I('get.page',1);
        $perpage = I('get.perpage',10);
        $admin = M('resEntity e')->field('e.rid,e.username,r.name,e.add_at,e.email,e.status')->
        join('pf_auth_group_access a on a.uid=e.rid')->
        join('pf_role r on r.id=a.role_id')->page($page,$perpage)->select();
        $this->assign('page',setPage(M('resEntity e')->join('pf_auth_group_access a on a.uid=e.rid')->join('pf_role r on r.id=a.role_id')->count(),$perpage,$page,'?'.http_build_query($url)));
        $this->assign('getData',$admin);
        $this->display('user/admin');
    }

    /*
     * 冻结&解封用户
     */
    public function adminDecate(){
        if(I('get.types')){
            M('ResEntity')->where(array('rid'=>I('get.resid')))->setField(array('status' => '1'));
            $this->showtips('成功解封该用户');
        }
        M('ResEntity')->where(array('rid'=>I('get.resid')))->setField(array(
            'status' => '0'
        ));
        $this->showtips('成功冻结该用户');
    }

    /*
     * 后台管理员管理
     * @date: 2016-8-2 上午10:40:23
     * @editor: liuqi
     */
    public function addAdmin(){
    	if(!is_login()){
    		redirect('/user/login');exit;
    	}
        /*用户注册逻辑*/
        if(I('post.submit')){
            if(I('post.rid')){
                $old_data = M('ResEntity')->where(['rid'=>I('post.rid')])->find();
                $update['update_at'] = time();
                if(I('post.password')){
                    $salt = randstr(5);
                    $data['password'] = md5(I('post.password').$salt);
                    $data['salt'] = $salt;
                }
                $data['username'] = I('post.username');
                if(I('post.role_id')){
                    M('AuthGroupAccess')->where(array('uid'=>I('post.rid')))->setField(array('role_id'=>I('post.role_id')));
                    // $data['role_id'] = I('post.role_id');
                }
                if($old_data['username'] != I('post.username')){
                    $update['mobile'] = I('post.username');
                }
                if(I('post.uname')){
                    $update['uname'] = I('post.uname');
                }
                M('ResEntity')->where(array('rid'=>I('post.rid')))->setField($data);
                putlog('修改管理员'.$data['username'].'的信息','UserController','addAdmin');
                $this->showtips('信息编辑成功');

            }
            $salts = randstr(5);
            $data = array(
                    'username' => I('post.username'),
                    'password' => md5(I('post.password').$salts),
                    'salt' => $salts,
                    'email' => I('post.email'),
                    'role_id' => I('post.role_id'),
                    'remember_token' => '',
                    'add_at' => time(),
                    'update_at' => time()
                );
            $res = M('ResEntity')->add($data);
            if(I('post.role_id')){
                M('AuthGroupAccess')->add(array('uid'=>$res,'role_id'=>I('post.role_id')));
            }
            if($res){
                $this->showtips('添加管理员成功');
            }else{
                $this->showtips('操作失败');
            }
        }
        $admin_info = M('resEntity e')->
        join('pf_auth_group_access a on a.uid=e.rid')->
        join('pf_role r on r.id=a.role_id')->where(array('e.rid'=>I('get.rid')))->find();
        $this->assign('admin_info',$admin_info);
        $role = M('role')->where(array('status'=>1))->select();
        $this->assign('role',$role);
        //注册页面渲染
        $this->display('user/add_admin');
    }


    /**
     * 前端注册用户列表
     */
    public function userList()
    {
        $page = I('get.page',1);
        $perpage = I('get.perpage',10);
        $url = [];
        $model = M('UserInfo');

        if(trim(I('get.user_name'))){
            $model = $model->where("user_name LIKE '%".trim(I('get.user_name'))."%'");
            $url['user_name'] = I('get.user_name');
        }
        if(trim(I('get.user_mobile'))){
            $model = $model->where(['user_mobile'=>trim(I('get.user_mobile'))]);
            $url['user_mobile'] = I('get.user_mobile');
        }
        $copy = clone $model;
        $getData = $model->order(['add_at' => 'desc'])->page($page, $perpage)->select();
        $this->assign('page', setPage($copy->count(), $perpage, $page, '?' . http_build_query($url)));
        $this->assign('getData', $getData);
        $this->display('user/list');
    }

    /**
     *更改前端用户登录状态
     * User: liuqi
     */
    public function setStatus()
    {
        if(I('get.types')){
            M('UserInfo')->where(array('user_id'=>I('get.uid')))->setField(array('status' => '1'));
            $this->showtips('成功解封该用户');
        }
        M('UserInfo')->where(array('user_id'=>I('get.uid')))->setField(array('status' => '0'));
        $this->showtips('成功冻结该用户');
    }
}
